Skip to content

fix(query_helper): patch date filter & sends 422 invalid date ops (BDMS-1523) - #1064

Open
TylerAdamMartinez wants to merge 1 commit into
stagingfrom
BDMS-1523-Created-At-First-Visit-Completed-Filter-Errors-Preview-Branch
Open

TylerAdamMartinez wants to merge 1 commit into
stagingfrom
BDMS-1523-Created-At-First-Visit-Completed-Filter-Errors-Preview-Branch

Conversation

@TylerAdamMartinez

Copy link
Copy Markdown
Contributor

Why

This PR addresses the following problem / context:

  • Date-based JSON filters require proper handling of SQLAlchemy Date and DateTime columns.
  • Filtering timestamp columns by date should match the entire calendar day rather than only timestamps at midnight.
  • Date filters need consistent input validation and support for common date formats.

How

Implementation summary: the following was changed / added / removed:

  • Added date-specific filtering logic in services/query_helper.py.
  • Added support for MM/DD/YYYY and YYYY-MM-DD date formats.
  • Added support for eq, ne, gt, gte, lt, lte, and in operators on date and timestamp columns.
  • Added validation that returns HTTP 422 for invalid date values and HTTP 400 for unsupported operators or invalid in arguments.

Author Checklist

  • Changes are limited to the intended scope.
  • Tests were added or updated where appropriate.
  • Relevant tests and lint checks pass.
  • No secrets, credentials, or sensitive data were publicly exposed.
  • Documentation was updated where necessary. (N/A)

Reviewer Checklist

  • Verify that both supported date formats are parsed correctly.
  • Verify that all supported comparison operators behave correctly for Date and DateTime columns.
  • Verify that invalid dates and unsupported operators return appropriate HTTP status codes.
  • Confirm that existing non-date filters and null operators remain unaffected.

Notes

Any special considerations, known limitations, risks, workarounds, or follow-up work to note?

  • Timestamp filtering uses UTC calendar days rather than the user's local timezone.
  • Date filters accept date-only values, not full datetime strings.

@TylerAdamMartinez TylerAdamMartinez self-assigned this Nov 19, 2026
@TylerAdamMartinez TylerAdamMartinez added the bug Something isn't working label Nov 19, 2026
@TylerAdamMartinez TylerAdamMartinez changed the title fix(query_helper): patch date filter & sends 422 invalid date ops fix(query_helper): patch date filter & sends 422 invalid date ops (BDMS-1523) Nov 19, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Coverage

✅ 82.15% total — gate is 75%.

Coverage for the Python files changed in this PR
Name Stmts Miss Cover Missing
services/query_helper.py 448 158 65% 110-123, 128-154, 161-163, 186-187, 193-194, 200, 227, 234, 243, 287, 305-361, 555, 559, 596-601, 621, 635, 639, 647, 732, 739-740, 744-751, 789, 792, 807-814, 817-818, 821, 823, 825, 836, 889, 892-893, 896-897, 901-908, 936-937, 950-954, 969-970, 981, 988-993, 996-1001, 1004-1009, 1015-1021, 1024-1030, 1033, 1036, 1039, 1042, 1050-1058, 1090, 1114, 1120, 1125-1126, 1149-1162
TOTAL 448 158 65%

@ksmuczynski ksmuczynski left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good! Few comments for consideration.

Also,docs/refine-json-filters-and-virtual-fields.md suggests operator changes should be documented there. Could you add a brief section covering the accepted date formats and the calendar-day behavior for timestamp columns?

Comment thread services/query_helper.py
Comment on lines +1011 to +1012
if is_date and operator not in {"null", "nnull"}:
return _apply_date_filter_clause(sql, column, operator, value)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this branch runs on every Date and DateTime column for routes using order_sort_filter (including Observation.observation_datetime, Sample.sample_date, and created_at across models), it also handles the ISO timestamp filters sent by the UI, and currently rejects them.
In OcotilloUI, observation/list.tsx defines observation_datetime and created_at as type: 'dateTime', and thing-id-link/list.tsx does the same for created_at. Since the data provider serializes filters with JSON.stringify, the API receives ISO 8601 strings like "2026-08-10T06:00:00.000Z". Previously, these requests failed in Postgres (operator does not exist: timestamp with time zone >= character varying), and now _parse_json_filter_date rejects them with a 422. While this isn't a regression, those filters still don't work, and this is the exact code path that could fix them.
Two ways we could handle this:

  1. Update _parse_json_filter_date to accept ISO 8601 datetimes (preserving calendar-day comparisons for date-only values and using exact comparisons for full datetimes), which would also fix the date filters in the observation and thing ID link grids.
  2. Leave this PR as is and open a follow-up ticket for ISO datetime support.

Either way, adding a test that filters /observation/groundwater-level by observation_datetime with an ISO timestamp would cover this case.

Comment thread services/query_helper.py
Comment on lines +248 to +249
start = datetime.combine(filter_date, time.min, timezone.utc)
end = start + timedelta(days=1)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a timezone mismatch between how the wells grid displays created_at and how this filter evaluates it:

  • Grid display: Uses formatAppDate with APP_TIMEZONE (which defaults to America/Denver).
  • Filter: Uses UTC day bounds.

Because of this, any record created after 6:00 PM MDT (or 5:00 PM MST) has a UTC date one day later than the date the grid shows. For example, a well created at 7:00 PM MDT on Oct 5 displays as "Oct 5, 2026" in the grid, yet filtering for 10/05/2026 doesn't return it and filtering for 10/06/2026 does.

Although the Notes call out UTC days, this mismatch between visible column values and filter results is likely to cause user confusion and bug reports about "missing" records. Could we use America/Denver day bounds here to match the grid, or link a follow-up ticket if UTC is intentional for now?

Comment thread services/query_helper.py
Comment on lines +199 to +203
if not isinstance(value, str):
raise HTTPException(
status_code=422,
detail="Date filter values must use MM/DD/YYYY or YYYY-MM-DD",
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor comment, but neither 422 message (here or at line 214) includes the field name, so a caller sending multiple filter params can't tell which one failed. Could _apply_json_filter_clause pass field through _apply_date_filter_clause so we can include it in the error?

Also minor, but worth noting, is null handling. Rejecting all non-string values means eq with null now returns a 422 rather than IS NULL. The UI sends null/nnull for empty checks, so current callers shouldn't be affected; just flagging the behavior change.

Comment thread tests/test_thing.py
.values(
first_visit_date=date(2026, 8, 10),
well_completion_date=date(2023, 6, 6),
created_at=datetime(2026, 10, 5, 14, 30, tzinfo=timezone.utc),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the fixture sets created_at to 14:30 UTC—9.5 hours away from either day boundary—these tests would still pass even if the day bounds were shifted by several hours. Off-by-a-few-hours bugs only show up near midnight, so adding a test case around 23:30 or 00:30 UTC would verify that the boundaries themselves are accurate.

I ran the new date tests against staging's query_helper.py and confirmed they all fail there except null/nnull, so the suite is definitely exercising the new behavior.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants